require('./index.scss');
var Tool = require('common/tool');
var Config = require('common/config');
var subMenuList = require('../subMenuList');
require('comp/tab');
var newBank;    //银行卡页面实例
var originalData = {
        addBank:{                   // 添加的银行相关的code信息，用作于接口参数
            bank:"ICBKCNBJXXX",     //银行名称code（ICBKCNBJXXX（中国工商银行），PCBCCNBJXXX（中国建设银行））
            bankAccountName:"",     //持卡人姓名
            bankOther:"",           //银行名称选择其他时，输入的文本框数据
            bankAccountNumber:"",   //银行账号
            bankAccountType:"",     //银行卡账户类型
            bankCurrency:"",        //币种 （对应数据为USD或CNY）
            internationalRemittanceCode:"", //国际汇款代码
            bankCountry:"ISO_3166_156",         //银行国家（中国对应数据为ISO_3166_716）
            bankCountryOther:"",    //银行国家，选择其他时，输入的数据
            bankProvince:"",        //银行地址(省)
            bankCity:"",            //银行地址(市)
            bankBranch:"",           //所属支行
            bankOrder:1              //标识第几个银行卡
        },
        addBankText:{               // 添加的银行相关的文本信息，用作于确认银行卡页面的显示
            bank:"中国工商银行",     //银行名称code（ICBKCNBJXXX（中国工商银行），PCBCCNBJXXX（中国建设银行））
            bankAccountType:"",     //银行卡账户类型
            bankCurrency:"",        //币种 （对应数据为USD或CNY）
            bankCountry:"中国",     //银行国家（对应数据为ISO_3166_716）
            bankProvince:"",        //银行地址(省)
            bankCity:"",            //银行地址(市)
        }
    }

var vm = avalon.define({
    $id: 'banklist',
    tabList:{
        active:1,
        list:subMenuList
    },
    step:1,                 // 第一步显示银行信息，第二步显示添加银行时的确认信息
    lang: _language=='zh' ? 'CN':'TW',
    uploadList:'',          // 需要上传资料的银行列表
    bankList:'',            // 已经拥有的银行列表
    bankData:{},
    bankActive:0,           // 默认展示的银行卡信息
    loginName:"",
    addBank: avalon.mix({},originalData.addBank),
    addBankText: avalon.mix({},originalData.addBankText),
    /* 在线客服 */
    contactfn:function(){
        var e = Config.CustOnline;
        window.open(e, "window1", "HEIGHT=420, WIDTH=565,top=0,left=0")
    },
    /* 重置 */
    reset:function(){
        var name = this.addBank.bankAccountName;
        this.addBank = avalon.mix({},originalData.addBank);
        this.addBankText = avalon.mix({},originalData.addBankText);
        this.addBank.bankAccountName = name;
        newBank.getDictConfigInfo();
        // var bank = this.addBank,
        //     addBankText = this.addBankText;
        // addBank.bank = "ICBKCNBJXXX";
        // addBank.bankOther = "";
        // addBank.bankAccountNumber = "";
        // addBank.internationalRemittanceCode = "";
        // addBank.internationalRemittanceCode = "";
        // addBank.bankCountry = "ISO_3166_156";
        // addBank.bankCountryOther = "";
        // addBankText.bankCountry = "中国";
        // addBankText.bank = "中国工商银行";
    },
    addBankFn:function(){
        var bank = this.addBank;
        if( bank.bankAccountNumber.length===0 ){
            layer.alert('请输入银行账号');
            return false;
        }else if( bank.bankCountry=='other' && bank.bankCountryOther.length===0 ){
            layer.alert('请输入银行国家');
            return false;
        }else if( bank.bank=='other' && bank.bankOther.length===0 ){
            layer.alert('请输入银行名称');
            return false;
        }
        this.step = 2;
    },
    changeSel:function(k,list){   // 切换银行字段的下拉框
        var that = this;
        setTimeout(function(){
            console.log('that.addBank[k]',that.addBank[k]);
            if(that.addBank[k]==''){
                that.addBankText[k]='';
                return false;
            }
            that[list].forEach(function(item,i) {
                if( item.code==that.addBank[k] ){
                    that.addBankText[k] = item['name'+that.lang];
                    console.log('that.addBankText[k]',that.addBankText[k]);
                    return false;
                }
            });
        },0);
        
    },
    /* 输入银行卡时格式化 */
    formatBankAccount:function(e){
        var reg = /(.{4})/g,
            v = e.target.value.replace(/\D+/g,'');
        setTimeout(function(){
            vm.addBank.bankAccountNumber = v.replace(reg,'$1 ').replace(/\s$/,'');
        },0);
    },
    /* 确认提交银行卡信息 */
    addBankSubmit:function(){
        newBank.addBankAjax();
    },
    /* 加载字典内容 */
    loadDict:function(){
        if( this.listBankList.length==0 ) newBank.getDictConfigInfo();
    },
    listBankList:'',        //银行资料中的银行列表
    listBankAccountType	:'',//账户类型
    listBankCurrency:'',    //账户币种
    listBanksCountry:'',    //银行国家
    listProvince:'',         //银行地址(省)
    listCities:''           //银行地址(市)
});


vm.$watch('onReady',function(){
    window['vm'] = vm;
    newBank = new Bank();
});
/* 切换国家时加载省份列表 */
vm.$watch('addBank.bankCountry',function(v){
    newBank.getProvinces();
});
/* 切换省份时加载城市列表 */
vm.$watch('addBank.bankProvince',function(v){
    newBank.getCities();
});



function Bank(){
    this.queryUrl = '/bankDetails';                             // 银行资料接口    
    this.queryDictConfigUrl = '/getDictConfigInfo';                 // 获取数据字典配置接口
    this.queryProvincesUrl = '/getProvincesByCountry';             // 根據國家获取对应的省份
    this.queryCitiesUrl = '/getCitiesByProvince';             // 根据省，获取对应的市
    this.addBankDetail = '/addBankDetail';                      // 添加银行
    this.queryInfo();
    
}
/* 获取用户银行卡列表 */
Bank.prototype.queryInfo = function(callback){
    var that = this;
    Tool.ajax({
        url: this.queryUrl,
        type:"post"
    }).done(function(res){

        var data = res.data;
        vm.uploadList = data.bankProposalList;
        vm.bankList = data.bankList;
        vm.bankActive=0;
        vm.addBank.bankAccountName = data.loginName;
        vm.bankData = data.bankList[0];
        if(data.bankList.length===0) that.getDictConfigInfo();
        callback && callback();
    });
}
/* 获取数据字典配置接口 */
Bank.prototype.getDictConfigInfo = function(){
    var that = this;
    Tool.ajax({
        url: this.queryDictConfigUrl,
        data:{
            dictCode:'BankAccountType,BankCurrency,listBankList,listBanksCountry'
        },
        type:"post"
    }).done(function(res){

        var data = res.data;
        vm.listBankList = data.listBankList;
        vm.listBankAccountType = data.BankAccountType;
        vm.listBankCurrency = data.BankCurrency;
        vm.listBanksCountry = data.listBanksCountry;
        that.getProvinces();

        vm.addBank.bankCurrency = data.BankCurrency.length>0 ? data.BankCurrency[0].code : ''
        vm.addBank.bankAccountType = data.BankAccountType.length>0 ? data.BankAccountType[0].code : ''
        vm.changeSel('bankCurrency','listBankCurrency');
        vm.changeSel('bankAccountType','listBankAccountType');
    });
}
/* 根據國家获取对应的省份  */
Bank.prototype.getProvinces=function(){
    var that = this;
    Tool.ajax({
        url: this.queryProvincesUrl,
        data:{
            country: vm.addBank.bankCountry
        },
        type:"post"
    }).done(function(res){

        var data = res.data;
        vm.listProvince = data.provinceList;
        vm.addBank.bankProvince = data.provinceList.length>0 ? data.provinceList[0].code : ''
        vm.changeSel('bankProvince','listProvince');
    });
}
/* 根据省，获取对应的市   */
Bank.prototype.getCities=function(){
    Tool.ajax({
        url: this.queryCitiesUrl,
        data:{
            province: vm.addBank.bankProvince
        },
        type:"post"
    }).done(function(res){

        var data = res.data;
        vm.listCities = data.cityList;
        vm.addBank.bankCity = data.cityList.length>0 ? data.cityList[0].code : ''
        vm.changeSel('bankCity','listCities');
    });
}

/* 添加银行卡确认 */
Bank.prototype.addBankAjax=function(){
    var params = $.extend({},vm.addBank.$model);
    var that = this;
    params.bankAccountNumber = params.bankAccountNumber.replace(/\D+/g,'');
    params.bankOrder = vm.bankList.length+1;
    var loading = layer.load(2,{shade:[.5, '#000']});
    Tool.ajax({
        url: this.addBankDetail,
        data: params,
        type:"post"
    }).done(function(res){
        layer.close(loading);

        vm.step=1;
        vm.reset();
        that.queryInfo(function(){
            vm.bankActive = vm.bankList.length-1;
            vm.bankData = data.bankList[vm.bankActive];
        });
        layer.alert('添加成功');
    });
}